package test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
 * TODO 线程练习
 *
 * @author david
 * @version 1.0
 * @date 2021/4/20 14:56
 */
public class ThreadDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Thread thread1 = new Thread1("Thread1-----------");
        Thread thread2 = new Thread2("Thread2");
        thread1.start();
        thread2.start();

        Thread thread3 = new Thread(new MyThread("thread3"));
        thread3.start();


        FutureTask<String> futureTask = new FutureTask<>(new MyThread2());
        new Thread(futureTask).start();
        System.out.println("线程返回值：" + futureTask.get());
    }

    static class Thread1 extends Thread {
        private final String title;

        public Thread1(String title) {
            this.title = title;
        }

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(title + ":" + i);
            }
        }
    }

    static class Thread2 extends Thread {
        private final String title;

        public Thread2(String title) {
            this.title = title;
        }

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(title + ":" + i);
            }
        }
    }

    static class MyThread implements Runnable {//线程主体类
        private final String title;

        public MyThread(String title) {
            this.title = title;
        }

        @Override
        public void run() {//线程的主体方法
            for (int x = 0; x < 10; x++) {
                System.out.println(this.title + "运行，x = " + x);
            }
        }
    }


    static class MyThread2 implements Callable<String> {
        @Override
        public String call() {
            for (int x = 0; x < 10; x++) {
                System.out.println("******线程执行，x = " + x);
            }
            return "线程执行完毕！";
        }
    }


}
